package com.cskaoyan.service;

import com.cskaoyan.bean.MarketKeyword;
import com.cskaoyan.bean.MarketKeywordExample;
import com.cskaoyan.mapper.MarketKeywordMapper;
import com.cskaoyan.util.MyBatisUtil;
import com.cskaoyan.util.StringUtil;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.List;

/**
 * @author bianyonglei
 * @since 2023/05/31 09:38
 */

public class AdminKeywordServiceImpl implements AdminKeywordService {
    @Override
    public List<MarketKeyword> list(String keyword, String url, int page, Integer limit, String sort, String order) {
        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria = marketKeywordExample.createCriteria();
        if (keyword != null) {
            criteria.andKeywordLike("%" + keyword + "%");
        }
        if (url != null) {
            criteria.andUrlLike("%" + url + "%");
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtil.isEmpty(sort) && !StringUtil.isEmpty(order)) {
            marketKeywordExample.setOrderByClause(sort + " " + order);
        }
        PageHelper.startPage(page, limit);
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketKeywordMapper keywordMapper = sqlSession.getMapper(MarketKeywordMapper.class);
        List<MarketKeyword> marketKeywords = keywordMapper.selectByExample(marketKeywordExample);
        sqlSession.close();
        return marketKeywords;
    }

    @Override
    public int create(MarketKeyword marketKeyword) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketKeywordMapper keywordMapper = sqlSession.getMapper(MarketKeywordMapper.class);
//        marketKeyword.setAddTime(new Date());
//        marketKeyword.setUpdateTime(new Date());
//        marketKeyword.setDeleted(false);
        int insert = keywordMapper.insertSelective(marketKeyword);

        sqlSession.commit();
        sqlSession.close();
        return insert;
    }

    @Override
    public int update(MarketKeyword marketKeyword) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketKeywordMapper keywordMapper = sqlSession.getMapper(MarketKeywordMapper.class);
        keywordMapper.updateByPrimaryKeySelective(marketKeyword);
        sqlSession.commit();
        sqlSession.close();
        return 200;
    }

    @Override
    public int delete(MarketKeyword marketKeyword) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketKeywordMapper keywordMapper = sqlSession.getMapper(MarketKeywordMapper.class);
        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();

        marketKeyword.setDeleted(true);
        marketKeywordExample.or().andIdEqualTo(marketKeyword.getId());
//        int row = keywordMapper.updateByPrimaryKeySelective(marketKeyword);
        keywordMapper.updateByExampleSelective(marketKeyword, marketKeywordExample);
        sqlSession.commit();
        sqlSession.close();
        return 200;
    }
}
